package com.kotlin.hxh.adapter

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import java.util.*

/**
 * @author huxh
 * @date 2018/12/17.
 */
class RecyclerViewHolder(val context: Context, itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {


    private val mViews: SparseArray<View> by lazy {
        SparseArray<View>()
    }

    private val objects: HashMap<String, Any> by lazy {
        hashMapOf<String, Any>()
    }

    companion object {
        @JvmStatic
        fun createViewHolder(Context: Context, itemView: View): RecyclerViewHolder {
            return RecyclerViewHolder(Context, itemView)
        }

        @JvmStatic
        fun createViewHolder(Context: Context, parent: ViewGroup, @LayoutRes layoutId: Int): RecyclerViewHolder {
            return createViewHolder(Context, LayoutInflater.from(Context).inflate(layoutId, parent, false))
        }
    }

    fun <T> getObject(key: String): T {
        return objects[key] as T
    }


    fun addObject(key: String, any: Any) {
        objects[key] = any
    }


    fun <T : View> getView(@IdRes viewId: Int): T {
        var view: View? = mViews[viewId]
        if (view == null) {
            view = itemView.findViewById(viewId)
            mViews.put(viewId, view)
        }
        return view!! as T
    }

    fun getView2(@IdRes viewId: Int): View {
        return getView<View>(viewId)
    }


    fun <T : View> getView(@IdRes viewId: Int, clazz: Class<T>): T {
        return getView<T>(viewId) as T
    }


    /****以下为辅助方法*****/

    /**
     * 设置TextView的值
     *
     * @param viewId
     * @param text
     * @return
     */
    fun setText(viewId: Int, text: String): RecyclerViewHolder {
        val tv = getView(viewId, TextView::class.java)
        tv.text = text
        return this
    }

    fun setImageResource(viewId: Int, resId: Int): RecyclerViewHolder {
        val view = getView(viewId, ImageView::class.java)
        view.setImageResource(resId)
        return this
    }

    fun setImageUrl(viewId: Int, url: String): RecyclerViewHolder {
        Glide.with(itemView.context).load(url).into(getView(viewId, ImageView::class.java))
        return this
    }

    fun setImageBitmap(viewId: Int, bitmap: Bitmap): RecyclerViewHolder {
        val view = getView(viewId, ImageView::class.java)
        view.setImageBitmap(bitmap)
        return this
    }

    fun setImageDrawable(viewId: Int, drawable: Drawable): RecyclerViewHolder {
        val view = getView(viewId, ImageView::class.java)
        view.setImageDrawable(drawable)
        return this
    }

    /**
     * 关于事件的
     */
    fun setOnClickListener(viewId: Int, listener: View.OnClickListener): RecyclerViewHolder {
        getView2(viewId).setOnClickListener(listener)
        return this
    }

}